package de.tabuma.pm.shopping;

import de.tabuma.shopping.entity.*;
import de.tabuma.shopping.controller.*;
import de.tabuma.shopping.logik.LicensePackageFacade;
import de.tabuma.pm.constants;

import de.tabuma.pm.project.logik.*;
import de.tabuma.pm.project.entity.*;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.google.appengine.api.users.*;

/**
 * @author$
 * @version$
 * @since$
 */
public class ShoppingController extends controller {
    
    private static final Logger log = Logger.getLogger(ShoppingController.class.getName());

    ShoppingFacade shoppingFacade;
    public LicensePackageFacade getLicensePackageFacade() {
        return shoppingFacade;
    }

    @Override
    public String getUrl() {
        return "http://localhost:8081/";
    }

    String orderNav;
    @Override
    public String getOrderNav() {
        return orderNav;
    }

    @Override
    public Class getAssignmentClass() {
        return Project.class;
    }

    @Override
    public List getAssignmentList() {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        List gList = ProjectFacade.getList(user.getEmail());
        return gList;
    }

    @Override
    public Field getAssignmentKeyField() {
        try {
            return Project.class.getDeclaredField("id");
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
        return null;
    }

    @Override
    public Field getAssignmentShowField() {
        try {
            return Project.class.getDeclaredField("name");
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
        return null;
    }

    @Override
    public String getAssignmentObjectname() {
        return "Project";
    }

    @Override
    public List<LicenseType> getLicenseTypeList() {
        List<LicenseType> list = new ArrayList<LicenseType>();
        LicenseType license = new LicenseType();
        license.setId(de.tabuma.pm.constants.PM_SHOPPING_VIEW_PUBLIC_LICENSE);
        license.setName("Public View License");
        list.add(license);
        license = new LicenseType();
        license.setId(de.tabuma.pm.constants.PM_SHOPPING_VIEW_PROTECTED_LICENSE);
        license.setName("Protected View License");
        list.add(license);
        return list;
    }

    @Override
    public Hashtable<Integer, String> getMethodHashtable() {
        Hashtable<Integer,String> methodHash = new Hashtable<Integer,String>();

        //methods for customer manipulation
        methodHash.put(constants.PM_SHOPPING_VIEW_PUBLIC_LICENSE, "show public plattform view");
        methodHash.put(constants.PM_SHOPPING_VIEW_PROTECTED_LICENSE, "show public protected view");
        
        return methodHash;
    }

    public ShoppingController(String servlet, Hashtable parameter, String orderNav) {
        super(servlet, parameter);
        shoppingFacade = new ShoppingFacade();
        this.orderNav = orderNav;
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        super.navigate(context, request, response, out);
    }

    @Override
    public String getValueFromProperties(String key) {
        return java.util.ResourceBundle.getBundle("de.tabuma.pm.shopping.language").getString(key);
    }


}